package jp.sourceforge.akjrcp.ui;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.util.FTPInfo;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;



public class FTPInfoDialog extends Dialog{
private FTPInfo ftpInfo=new FTPInfo();
private Text connectionName,dir,userName,password,hostName,port;
	protected FTPInfoDialog(Shell parentShell) {
		super(parentShell);
		// TODO Auto-generated constructor stub
	}
	
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite)super.createDialogArea(parent);
		composite.setLayout(new GridLayout(2,false));
		createLabel(composite,Messages.getString("ftpInfo.connection_name"));
		connectionName=new Text(composite,SWT.SINGLE);
		connectionName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		connectionName.setText(ftpInfo.getName());
		
		createLabel(composite,Messages.getString("ftpInfo.host"));
		hostName=new Text(composite,SWT.SINGLE);
		hostName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		hostName.setText(ftpInfo.getHost());
		
		createLabel(composite,Messages.getString("ftpInfo.user"));
		userName=new Text(composite,SWT.SINGLE);
		userName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		userName.setText(ftpInfo.getUser());
		
		createLabel(composite,Messages.getString("ftpInfo.password"));
		password=new Text(composite,SWT.PASSWORD);
		password.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		password.setText(ftpInfo.getPassword());
		
		createLabel(composite,Messages.getString("ftpInfo.dir"));
		dir=new Text(composite,SWT.SINGLE);
		dir.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		dir.setText(ftpInfo.getDir());
		
		createLabel(composite,Messages.getString("ftpInfo.port"));
		port=new Text(composite,SWT.SINGLE);
		port.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		if(ftpInfo.getPort()!=0){
		port.setText(""+ftpInfo.getPort());
		}
		return composite;
	}
	
    protected void okPressed() {
        setReturnCode(OK);
        
   
        
        ftpInfo.setName(connectionName.getText());
        ftpInfo.setHost(hostName.getText());
        ftpInfo.setUser(userName.getText());
        ftpInfo.setPassword(password.getText());
        
        ftpInfo.setDir(dir.getText());
        try{
        ftpInfo.setPort(Integer.parseInt(port.getText()));
        }catch(Exception e){
        	//should check.
        }
        
        close();
    }
    
	private void createLabel(Composite parent,String label){
		Label userLabel=new Label(parent,SWT.NULL);
		userLabel.setText(label);
	}
	
	protected void createButtonsForButtonBar(Composite parent) {
		
	    createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
	            false);
	    
	    createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL,
	            true);
	}

	public FTPInfo getFtpInfo() {
		return ftpInfo;
	}

	public void setFtpInfo(FTPInfo newInfo) {
		ftpInfo.copyValue(newInfo);
	}
}
